package com.oamicnet.mapstruct.service;

import com.oamicnet.mapstruct.controller.dto.UserDTO;
import com.oamicnet.mapstruct.dao.UserDao;
import com.oamicnet.mapstruct.entity.User;
import com.oamicnet.mapstruct.mapper.UserMapper;

public class UserService {

    public void saveUser(UserDTO userDTO){
        UserDao userDao = new UserDao();

        //1-需要手动的将userDTO转为DO
//        User user = new User();
//        user.setName(userDTO.getName());
//        user.setId(userDTO.getId());

        //2-可以借助BeanUtil这个工具类继续转
//        try {
//            User user = new User();
//            BeanUtils.copyProperties(user, userDTO);
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        } catch (InvocationTargetException e) {
//            throw new RuntimeException(e);
//        }
        //3-用MapStruct进行转换
        User user = UserMapper.INSTANCE.userDTOToUser(userDTO);
        userDao.saveUser(user);
    }

    public UserDTO findUserById(Long id){
        UserDao userDao = new UserDao();
        User user = userDao.getUserById(id);

        //手动的讲DO转为DTO
//        UserDTO userDTO = new UserDTO();
//        userDTO.setId(user.getId());
//        userDTO.setName(user.getName());
        UserDTO userDTO = UserMapper.INSTANCE.userToUserDTO(user);
        return userDTO;
    }
}
